<?php

namespace App\Repositories;
use Cache;
use App\Models\TestingItem;
class TestingItemRepository extends Repository{

    static $tag = 'testing.item';

    static $tableName = 'testing_item';

    public function store($data)
    {
        $res = TestingItem::firstOrCreate($data);
        return $res;
    }

    public function getById($id)
    {
        $cacheKey = getCacheKey('testing.cache_key.testing_item.info');
        $cate = $this->remember($cacheKey.$id, function () use ($id) {
            return TestingItem::where('id', $id)->firstOrFail();
        });
        return $cate;
    }

    public function getList(){
        $key = getCacheKey('testing.cache_key.testing_item.list');
        $list = $this->remember($key,function(){
            return TestingItem::all();
        });
        return $list;
    }

    public function batchInsertData($data){
        TestingItem::batchInsertData(self::$tableName,$data);
    }


    public function convertQuestionOptions($question){
        $arr = explode("\n",$question);
        $options = [];
        foreach ($arr as $option){
            $optionArr = explode('##',$option);
            if(count($optionArr)>2){
                $obj['key'] = $optionArr[0];
                //$weight = $optionArr[1]?str_replace(';','',$optionArr[1]):0;
                $obj['weight'] = $optionArr[1];
                $jump = $optionArr[2]?str_replace(';','',$optionArr[2]):0;
                $obj['jump']= (int)$jump;
                array_push($options,$obj);
            }elseif(count($optionArr)>0){
                $obj['key'] = $optionArr[0];
                $weight = $optionArr[1]?str_replace(';','',$optionArr[1]):0;
                $obj['weight'] = $weight;
                array_push($options,$obj);
            }
        }
        return json_encode($options, JSON_UNESCAPED_UNICODE);
    }

    /**获取第四种类型expression
     * @param $tid
     * @return mixed
     */
    public function typeFourItem($tid){
        $key = getCacheKey('testing.cache_key.categroy.item.typefour');
        $key = $key.$tid;
        $result = $this ->remember($key,function() use($tid){
            $question_type=4;
            $item = TestingItem::select('index','expression as relateExpress')
                ->where('testing_id',$tid)
                ->where('question_type',$question_type)
                ->orderBy('index','asc')
                ->get()->toarray();
            return $item;
        });
        return $result;
    }

    /**获取第四种类型expression
     * @param $tid
     * @return mixed
     */
    public function getItemsByQuestionType($tid,$types,$keySuffix='_key'){
        $key = getCacheKey('testing.cache_key.categroy.item.typefour').$keySuffix;
        $key = $key.$tid;
        $result = $this ->remember($key,function() use($tid,$types){
            $item = TestingItem::select('index','expression')
                ->where('testing_id',$tid)
                ->where('question_type',$types)
                ->orderBy('index','asc')
                ->get()->toarray();
            return $item;
        });
        return $result;
    }

    /**
     * 根据tid和index查找数据
     * @param $tid
     * @param $index
     * @return mixed
     */
    public function getItemsByTidAndIndex($tid, $index){
        return TestingItem::select('index', 'options', 'question_type', 'expression')
            ->where('testing_id', $tid)
            ->whereIn('index', $index)
            ->orderBy('index', 'asc')
            ->get()->toarray();
    }

    public function model()
    {
        return app(TestingItem::class);
    }

    public function tag()
    {
        return self::$tag;
    }

    public function cacheTime()
    {
        return config('testing.common_cache_time');
    }
}